package com.shundr.shipper.cargo.view;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shundr.shipper.R;
import com.shundr.shipper.base.BaseFullScreenDialog;
import com.shundr.shipper.cargo.adapter.CargoTemplateAdapter;
import com.shundr.shipper.cargo.adapter.CargoTemplateAdapter.OnTemSelectListener;
import com.shundr.shipper.cargo.http.CargoRequest;
import com.shundr.shipper.cargo.model.CargoTemplateInfo;
import com.shundr.shipper.common.util.ProgressDialogHelper;
import com.shundr.shipper.common.util.ToastHelper;
import com.shundr.shipper.common.view.CListView;
import com.shundr.shipper.common.view.CListView.OnLoadMoreListener;
import com.shundr.shipper.common.view.CListView.OnRefreshListener;
import com.shundr.shipper.common.view.DialogDelete;
import com.shundr.shipper.common.view.DialogDelete.onDeleteListener;
import com.shundr.shipper.frame.constant.LContext;
import com.shundr.shipper.frame.http.LHttpRequest;

/**
 * 
 * DialogAuth
 * 
 * @author larry 2015-4-16 下午7:04:38 
 * @version 1.0.0
 *
 */
public class DialogTemplateCargo extends BaseFullScreenDialog {
	
	private Context mContext;
	private String mTitle;
	
	private OnSelectedListener selectedListener;
	
	private CListView mListView;
	private RelativeLayout mLayoutNoContent;
	private TextView tvNoContent;
//	
	private static final int PAGE_SIZE = LContext.PAGE_SIZE;
	private int pageNo = 0;
//	
	private CargoTemplateAdapter mAdapter;
//	
	private List<CargoTemplateInfo> mList = new ArrayList<CargoTemplateInfo>();
	
	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			try {
				switch (msg.what) {
				case CargoRequest.GET_CARGO_TEMPLATE_LIST_SUCCESSED:
					@SuppressWarnings("unchecked")
					List<CargoTemplateInfo> infos = (List<CargoTemplateInfo>) msg.obj;
					if (infos != null) {
						if (infos.size() > 0) {
							if (pageNo == 0) {
								mList.clear();
								mListView.setVisibility(View.VISIBLE);
								mLayoutNoContent.setVisibility(View.GONE);
								if (infos.size() >= PAGE_SIZE) {
									mListView.setCanLoadMore(true);
									mListView.setAutoLoadMore(true);
									mListView.setCanRefresh(true);
								} else {
									mListView.setCanLoadMore(false);
									mListView.setAutoLoadMore(false);
									mListView.setCanRefresh(false);
								}
							}
							mList.addAll(infos);
							mAdapter.notifyDataSetChanged();
							if (pageNo == 0) {
								mListView.setSelection(0);
							}
							pageNo = pageNo + 1;
						} else {
							if (pageNo == 0) {
								mListView.setVisibility(View.GONE);
								mLayoutNoContent.setVisibility(View.VISIBLE);
							} else {
								ToastHelper.toastLong(mContext, "最后一页");
							}
						}
					}
					ProgressDialogHelper.dismiss();
					mListView.onRefreshComplete();
					mListView.onLoadMoreComplete();
					break;
				case LHttpRequest.REQUEST_FAILED:
					ProgressDialogHelper.dismiss();
					mListView.onLoadMoreComplete();
					mListView.onRefreshComplete();
					ToastHelper.toastShort(mContext, msg.obj.toString());
					break;

				default:
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			super.handleMessage(msg);
		}
	};
	
	public DialogTemplateCargo(Context context,String title) {
		super(context);
		this.mContext = context;
		this.mTitle = title;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle(mTitle);
		mView = LayoutInflater.from(mContext).inflate(R.layout.activity_cargo_template_list, null);
		initMainView();
		setMainView(mView);
	}
	
	private void initMainView(){
		btnRight.setVisibility(View.INVISIBLE);
		mListView = (CListView) mView.findViewById(R.id.lv_list);
		mLayoutNoContent = (RelativeLayout)mView.findViewById(R.id.layout_no_content);
		tvNoContent = (TextView)mView.findViewById(R.id.tv_no_content);
		tvNoContent.setText("您还没有订单");
		mAdapter = new CargoTemplateAdapter(mList, mContext,true);
		mListView.setAdapter(mAdapter);
		
		mAdapter.setSelectListener(new OnTemSelectListener() {
			
			@Override
			public void onSelect(int position) {
				selectedListener.onSelected(mList.get(position));
				dismiss();
			}
		});
		mListView.setCanLoadMore(false);
		mListView.setAutoLoadMore(false);
		mListView.setCanRefresh(true);
		mListView.setOnRefreshListener(new OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				pageNo = 0;
				new CargoRequest(mContext, mHandler).getCargoTemplateList(pageNo * PAGE_SIZE, PAGE_SIZE);
			}
		});
		mListView.setOnLoadListener(new OnLoadMoreListener() {
			@Override
			public void onLoadMore() {
				new CargoRequest(mContext, mHandler).getCargoTemplateList(pageNo * PAGE_SIZE, PAGE_SIZE);
			}
		});
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
//				try {
//					Intent intent = new Intent(mContext,CargoTemplateDetailActivity.class);
//					intent.putExtra("json",JsonUtil.toJson(mList.get(position - 1)));
//					mContext.startActivity(intent);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
			}
		});
		mListView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					final int position, long id) {
				CargoTemplateInfo info = mList.get(position - 1);
				if(info!=null){
					final String md5Id=info.getMd5Id(); 
					DialogDelete dialogDelete=new DialogDelete(mContext);
					dialogDelete.setOnDeleteListener(new onDeleteListener() {
						
						@Override
						public void delete() {
							new CargoRequest(mContext, new Handler(){

								@Override
								public void handleMessage(Message msg) {
									super.handleMessage(msg);
									switch (msg.what) {
									case CargoRequest.DELETE_CARGO_TEMPLATE_SUCCESSED:
										ToastHelper.toastShort(mContext, "删除成功");
										mList.remove(position-1);
										mAdapter.notifyDataSetChanged();
										if(mList.size() == 0){
											mListView.setVisibility(View.GONE);
											mLayoutNoContent.setVisibility(View.VISIBLE);
										}
										break;
									case LHttpRequest.REQUEST_FAILED:
						                ToastHelper.toastShort(mContext, msg.obj.toString());
										break;
									default:
										break;
									}
								}
								
							}).deleteCargoTemplate(md5Id);
						}
					});
					dialogDelete.show();
				}
				return true;
			}
		});
		
		new CargoRequest(mContext, mHandler).getCargoTemplateList(pageNo * PAGE_SIZE, PAGE_SIZE);
	}
	
	public void setSelectedListener(OnSelectedListener selectedListener) {
		this.selectedListener = selectedListener;
	}

	public interface OnSelectedListener{
		public void onSelected(CargoTemplateInfo info);
	}
	
}
